package org.eclipsecon.dui.example;

import java.io.IOException;
import java.util.Collections;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipsecon.dui.interpreter.IContextAware;
import org.eclipsecon.dui.interpreter.IModelContext;

public class SaveAction implements SelectionListener, IContextAware {

	private IModelContext context;

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		if ( context.getModel() == null) {
			MessageDialog.openError(Display
					.getDefault().getActiveShell(), "Nothing to save",
					"Load a model prior to save");
			return;
		}
		try {
			boolean openConfirm = MessageDialog.openConfirm(Display
					.getDefault().getActiveShell(), "Save Model",
					"Do you really want to save your model?");
			if (openConfirm) {
				context.getModel().eResource().save(Collections.emptyMap());
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	@Override
	public void setContext(IModelContext context) {
		this.context = context;

	}
}